package com.example.leetcode.problem.isuniquelcci;

import java.util.Arrays;

public class RotateMatrixLcci {
    /**
     * 面试题 01.07. 旋转矩阵
     * 给你一幅由 N × N 矩阵表示的图像，其中每个像素的大小为 4 字节。请你设计一种算法，将图像旋转 90 度。
     * <p>
     * 不占用额外内存空间能否做到？
     * <p>
     * 示例 1:
     * <p>
     * 给定 matrix =
     * [
     * [1,2,3],
     * [4,5,6],
     * [7,8,9]
     * ],
     * <p>
     * 原地旋转输入矩阵，使其变为:
     * [
     * [7,4,1],
     * [8,5,2],
     * [9,6,3]
     * ]
     * 示例 2:
     * <p>
     * 给定 matrix =
     * [
     * [ 5, 1, 9,11],
     * [ 2, 4, 8,10],
     * [13, 3, 6, 7],
     * [15,14,12,16]
     * ],
     * <p>
     * 原地旋转输入矩阵，使其变为:
     * [
     * [15,13, 2, 5],
     * [14, 3, 4, 1],
     * [12, 6, 8, 9],
     * [16, 7,10,11]
     * ]
     *
     * @param matrix
     */
    public void rotate(int[][] matrix) {
        int n = matrix.length;
        int[][] matrix_new = new int[n][n];
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                matrix_new[j][n - i - 1] = matrix[i][j];
            }
        }
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                matrix[i][j] = matrix_new[i][j];
            }
        }
    }

    /**
     * 解题方法
     * https://leetcode-cn.com/problems/rotate-matrix-lcci/solution/xuan-zhuan-ju-zhen-by-leetcode-solution/
     *
     * @param args
     */
    public static void main(String[] args) {
        RotateMatrixLcci test = new RotateMatrixLcci();
        int n = 3;
        int[][] matrix_new = new int[n][n];
        // [[1,2,3],[4,5,6],[7,8,9]]
        matrix_new[0][0] = 1;
        matrix_new[0][1] = 2;
        matrix_new[0][2] = 3;

        matrix_new[1][0] = 4;
        matrix_new[1][1] = 5;
        matrix_new[1][2] = 6;

        matrix_new[2][0] = 7;
        matrix_new[2][1] = 8;
        matrix_new[2][2] = 9;

        test.rotate(matrix_new);
        System.out.println(Arrays.deepToString(matrix_new));
    }


}
